# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# To build with Qt-Creator:
# 1. Launch Qt-Creator with the env (set from cmd.exe):
# set "MINGW_PREFIX=C:/msys64/mingw64"
# set "ACLOCAL_PATH=%MINGW_PREFIX%/share/aclocal:/usr/share/aclocal"
# set "PKG_CONFIG_PATH=%MINGW_PREFIX%/lib/pkgconfig"
# set "PATH=%MINGW_PREFIX%/bin;%PATH%"
# 2. Specify the following as the CMake arguments:
# -DMINGW_LIBS=%MINGW_PREFIX% -DCMAKE_BUILD_TYPE=Debug -DWITH_SYSTEM_GLEW=ON -DWITH_SYSTEM_OPENJPEG=ON -DWITH_INSTALL_PORTABLE=OFF -DWITH_OPENCOLLADA=ON -DWITH_GAMEENGINE=ON -DWITH_RAYOPTIMIZATION=ON -DWITH_JACK=OFF -DWITH_PLAYER=ON -DWITH_CODEC_FFMPEG=ON -DWITH_CODEC_SNDFILE=ON -DWITH_CYCLES=ON -DWITH_CYCLES_CUDA_BINARIES=OFF -DWITH_CYCLES_OSL=ON -DWITH_FFTW3=ON -DWITH_MOD_OCEANSIM=ON -DPYTHON_VERSION=3.7 -DPYTHON_LIBPATH=%MINGW_PREFIX%/lib -DPYTHON_LIBRARY="python3.6m" -DPYTHON_INCLUDE_DIRS=/msys64/mingw64/include/python3.6m -DWITH_PYTHON_INSTALL=OFF -DWITH_PYTHON_INSTALL_NUMPY=OFF -DPYTHON_NUMPY_PATH=%MINGW_PREFIX%/lib/python3.6/site-packages -DCYCLES_OSL=%MINGW_PREFIX% -DWITH_IMAGE_REDCODE=ON -DWITH_OPENMP=ON -DWITH_LLVM=ON -DLLVM_STATIC=ON

_realname=blender
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.6.0
pkgrel=3
pkgdesc="A fully integrated 3D graphics creation suite (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
license=('spdx:GPL-2.0-or-later')
# build conflict with "${MINGW_PACKAGE_PREFIX}-glog"
url="https://www.blender.org"
depends=("${MINGW_PACKAGE_PREFIX}-alembic"
         #"${MINGW_PACKAGE_PREFIX}-audaspace"
         #"${MINGW_PACKAGE_PREFIX}-bullet" blender required bullet to built with USE_DOUBLE_PRECISION=ON
         "${MINGW_PACKAGE_PREFIX}-embree"
         "${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-gmp"
         "${MINGW_PACKAGE_PREFIX}-ffmpeg"
         "${MINGW_PACKAGE_PREFIX}-fftw"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         #"${MINGW_PACKAGE_PREFIX}-gflags"
         #"${MINGW_PACKAGE_PREFIX}-glog"
         "${MINGW_PACKAGE_PREFIX}-icu"
         "${MINGW_PACKAGE_PREFIX}-imath"
         #"${MINGW_PACKAGE_PREFIX}-jemalloc"
         "${MINGW_PACKAGE_PREFIX}-libepoxy"
         "${MINGW_PACKAGE_PREFIX}-libharu"
         "${MINGW_PACKAGE_PREFIX}-libiconv"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libsndfile"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-libwebp"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread-git"
         "${MINGW_PACKAGE_PREFIX}-lzo2"
         "${MINGW_PACKAGE_PREFIX}-omp"
         "${MINGW_PACKAGE_PREFIX}-openal"
         "${MINGW_PACKAGE_PREFIX}-opencollada"
         "${MINGW_PACKAGE_PREFIX}-opencolorio"
         "${MINGW_PACKAGE_PREFIX}-openexr"
         $([[ ${CARCH} == aarch64 ]] || echo "${MINGW_PACKAGE_PREFIX}-openimagedenoise")
         "${MINGW_PACKAGE_PREFIX}-openimageio"
         "${MINGW_PACKAGE_PREFIX}-openjpeg2"
         "${MINGW_PACKAGE_PREFIX}-openpgl"
         "${MINGW_PACKAGE_PREFIX}-openshadinglanguage"
         "${MINGW_PACKAGE_PREFIX}-opensubdiv"
         "${MINGW_PACKAGE_PREFIX}-openvdb"
         "${MINGW_PACKAGE_PREFIX}-openxr-sdk"
         "${MINGW_PACKAGE_PREFIX}-potrace"
         "${MINGW_PACKAGE_PREFIX}-pugixml"
         "${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-python-numpy"
         "${MINGW_PACKAGE_PREFIX}-SDL2"
         "${MINGW_PACKAGE_PREFIX}-tbb"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-zstd")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-boost"
             "${MINGW_PACKAGE_PREFIX}-eigen3"
             "${MINGW_PACKAGE_PREFIX}-llvm"
             "${MINGW_PACKAGE_PREFIX}-wintab-sdk"
             $([[ "$CARCH" == "aarch64" ]] && echo "${MINGW_PACKAGE_PREFIX}-sse2neon"))
install=blender-${MSYSTEM}.install
source=(https://download.blender.org/source/${_realname}-${pkgver}.tar.xz
        blender-addons.tar.gz::https://projects.blender.org/blender/blender-addons/archive/v${pkgver}.tar.gz
        0001-mingw-buildsystem.patch
        0002-fix-mingw-w64-definitions.patch
        0003-disable-static_assert_align-on-mingw.patch
        0005-fix-WINTAB_PRINTF-on-mingw.patch
        0006-mingw-DEBUG-libmv-avoid-too-many-sections.patch
        0007-disable-compiler-warnings.patch
        0008-mingw-use-X11-like-path-layout.patch
        0009-include-missing-headers.patch
        0010-do-not-link-to-extern_bullet.patch
        0011-no-TIME_UTC-on-msvcrt.patch
        0015-mingw-python-like-msvc.patch
        0016-mingw-windres.patch
        0019-fix-casts.patch
        0020-blendthumb-build.patch
        0022-find-llvm-convert-paths.patch
        0023-link-errors.patch
        0024-openvdb-shared.patch
        0025-callback-function-prototype-fix.patch
        0028-use-std-min-max.patch
        0032-fix-finding-tbb.patch
        0033-dont-add-fno-rtti-compile-option.patch
        0034-Fix-ARM64-Build.patch
        0035-Enable-SSE2NEON-on-Windows-ARM.patch
        0039-no-WRL-on-mingw-w64.patch
        0040-missing-header.patch)
sha256sums=('4b3756cb374686c69eb2fd555f96547d49cb1ef46f5bc6ee25394e571cfac8e9'
            '1cbe4955857654dc7e0278d9121072dafe0c7ec73f8ba053a7cb48e7e87c16ee'
            'cca53ed364edd6aa2edb1792fcf665a26fd2450bc0f45ecf018a04b35fae7769'
            'e05513221a4926ce3eb67ae5fc7c34d9863d7cc36755450807c437b197c25a88'
            'ae79efa161469598d9a3901a27c1552d4d0b104f032b233dc6883d25c50e1059'
            'a8ae50b48f591a13147d65c3455f204207589346bae635135a9bf9cf78b9234e'
            '1ccbf571e8dae23d0bff4ac848825788ac0a838261c5b02578c30852061238c8'
            '94b1299ff8fff061e1387e5ed14b4c1c58ae834c30c3360a0c53aaadf226064f'
            '9822ab6ab47fb67e8a75df44c3a7c3cdd060ad0c63acb87eeeea0be53bb0f252'
            '02617b6488c7e703cbebc5e50da231a605e69673491eeb9a2a07949393079e8e'
            'b080e86dd8042864ccda3a53a066d04dbc7038d7d16359c588f8d1f6063038a3'
            'bb28ef424c63a53b788255e8752359313933a87bbe802764eb5d8aedf3ca525d'
            '149031269e20aa731d61238f68df77e992516f7fe97af2f25bda244c8cc35c08'
            'dff3f13741c6dd177fb887731ccd51cad0a90695ceeca715bc322a87b4170662'
            'a1a2e9aa9950e4438576291518c649074b6a0c473429bc16494995a981de614b'
            '09ded3d921e595f70d47d651bb8b287457236987eb694003e114187a6a4c324b'
            '8be734582b9980ad0a3c5a82c200767ce558084f18e3cca11947991a14b0d68a'
            'c3e311a713a78a00a158cbacbf71de479872faee92282a9ebbcb3099af796037'
            'c39bfb8c5e4f6d26601e9cb03b979265ff0bbbcb2d2758005a4d0b0ed9616c7f'
            '37927b5a4e93461343841c84bdd1c67755bd03ac14d454ad8f4a952c37049ff7'
            '4a22fb3fcb3448ab0868e478fad19985ad079582f01e23317f3ddcdf6c0d49aa'
            'd24fab2e66b6fa736923cbe765a52162880cdd71dce179f9aa50ec251955c31f'
            '16b560363717357a0e69d3b46269cace9bf978126e08b604ad18a532a8dfd0b2'
            'a48e49b9e6034ccd3b448bc44f4f30a4d64682e8820f68d470069c9cbcf8b6c7'
            '439c4d0978e3690f42a2c7b84a9fa91ed9b27979f244d4e8edd1461fe20d7011'
            '31366f4856b00983f6e73135ecadc0776ea06ff2966c7fecad331b8fb4467112'
            '91e0347aaca5e867d5c743c402fa24e60f2b68272b91ffdaef5e7e45ece11ea4')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}

  rm -rf scripts/addons && mv "${srcdir}"/blender-addons scripts/addons

  rm -f build_files/cmake/platform/platform_win32_mingw64.cmake
  rm -f release/windows/icons/blender.exe.manifest

  apply_patch_with_msg \
    0001-mingw-buildsystem.patch \
    0002-fix-mingw-w64-definitions.patch \
    0003-disable-static_assert_align-on-mingw.patch \
    0005-fix-WINTAB_PRINTF-on-mingw.patch \
    0006-mingw-DEBUG-libmv-avoid-too-many-sections.patch \
    0007-disable-compiler-warnings.patch \
    0008-mingw-use-X11-like-path-layout.patch \
    0009-include-missing-headers.patch \
    0011-no-TIME_UTC-on-msvcrt.patch \
    0015-mingw-python-like-msvc.patch \
    0016-mingw-windres.patch \
    0019-fix-casts.patch \
    0020-blendthumb-build.patch \
    0022-find-llvm-convert-paths.patch \
    0023-link-errors.patch \
    0024-openvdb-shared.patch \
    0025-callback-function-prototype-fix.patch \
    0028-use-std-min-max.patch \
    0032-fix-finding-tbb.patch \
    0033-dont-add-fno-rtti-compile-option.patch \
    0034-Fix-ARM64-Build.patch \
    0035-Enable-SSE2NEON-on-Windows-ARM.patch \
    0039-no-WRL-on-mingw-w64.patch \
    0040-missing-header.patch
}

build() {
  mkdir -p "${srcdir}"/build-${MSYSTEM}  && cd "${srcdir}"/build-${MSYSTEM}

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  CXXFLAGS+=" -w"
  CFLAGS+=" -w"
  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    CXXFLAGS+=" -Wno-c++11-narrowing"
    CFLAGS+=" -Wno-unreachable-code-generic-assoc"
  fi

  if [[ $CARCH == "aarch64" ]]; then
    _extra_config+=("-DSSE2NEON_ROOT_DIR=${MINGW_PREFIX}/include/sse2neon")
  fi

  export XR_OPENXR_SDK_ROOT_DIR=${MINGW_PREFIX}
  export EMBREE_ROOT_DIR=${MINGW_PREFIX}

  _pyver=$(${MINGW_PREFIX}/bin/python -c "import sys;sys.stdout.write('.'.join(map(str, sys.version_info[:2])))")

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${_extra_config[@]}" \
    -DWITH_SYSTEM_AUDASPACE=OFF \
    -DWITH_SYSTEM_GFLAGS=OFF \
    -DWITH_SYSTEM_GLOG=OFF \
    -DWITH_SYSTEM_BULLET=OFF \
    -DWITH_SYSTEM_LZO=ON \
    -DWITH_SYSTEM_EIGEN3=ON \
    -DWITH_SYSTEM_FREETYPE=ON \
    -DWITH_INSTALL_PORTABLE=OFF \
    -DWITH_OPENIMAGEIO=ON \
    -DWITH_CYCLES=ON \
    -DWITH_CYCLES_STANDALONE=ON \
    -DWITH_CYCLES_STANDALONE_GUI=ON \
    -DWITH_CYCLES_CUDA_BINARIES=OFF \
    -DWITH_CYCLES_OSL=ON \
    -DWITH_MEM_JEMALLOC=OFF \
    -DWITH_MOD_OCEANSIM=ON \
    -DPYTHON_VERSION=${_pyver} \
    -DPYTHON_LIBPATH=${MINGW_PREFIX}/lib \
    -DPYTHON_LIBRARY=${MINGW_PREFIX}/lib/libpython${_pyver}.dll.a \
    -DPYTHON_INCLUDE_DIR=${MINGW_PREFIX}/include/python${_pyver} \
    -DWITH_PYTHON_INSTALL=OFF \
    -DWITH_PYTHON_INSTALL_NUMPY=OFF \
    -DWITH_OPENMP=ON \
    -DWITH_LLVM=ON \
    -DLLVM_STATIC=ON \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd ${srcdir}/build-${MSYSTEM}

  DESTDIR=${pkgdir} ${MINGW_PREFIX}/bin/cmake --install .

  cp -r bin ${pkgdir}${MINGW_PREFIX}/
  rm ${pkgdir}${MINGW_PREFIX}/bin/msgfmt.exe

  ${MINGW_PREFIX}/bin/python -m compileall -o 0 -o 1 -q "${pkgdir}${MINGW_PREFIX}/share/blender/${pkgver%.*}"
  ${MINGW_PREFIX}/bin/python -m compileall -o 0 -o 1 -q \
    ${pkgdir}${MINGW_PREFIX}/share/blender/${pkgver%.*}/scripts/startup \
    ${pkgdir}${MINGW_PREFIX}/share/blender/${pkgver%.*}/scripts/modules \
    ${pkgdir}${MINGW_PREFIX}/share/blender/${pkgver%.*}/scripts/addons

  install -dm755 "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/"
  for file in "${srcdir}/${_realname}-${pkgver}/doc/license/"*; do
    install -Dm644 "${file}" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/"
  done
}
